﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using ZCustomControl.Models;
using MS.Internal.Commands;
using MS.Internal.KnownBoxes;
using MS.Internal.PresentationFramework;

namespace ZCustomControl.Controls
{
    public class SortButton : Control
    {
        #region Fields

        private Button _btnAsc = null;
        private Button _btnDes = null;

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty CurrentSortModeProperty = DependencyProperty.Register("CurrentSortMode", typeof(SortMode), typeof(SortButton),
            new FrameworkPropertyMetadata(SortMode.Asc, FrameworkPropertyMetadataOptions.None, OnCurrentSortModePropertyChanged, null, false, UpdateSourceTrigger.PropertyChanged));

        #endregion

        #region Property Wrappers

        public SortMode CurrentSortMode
        {
            get { return (SortMode)GetValue(CurrentSortModeProperty); }
            set { SetValue(CurrentSortModeProperty, value); }
        }

        #endregion

        #region Routed Events

        public static readonly RoutedEvent SortModeChangedEvent
            = EventManager.RegisterRoutedEvent("SortModeChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SortButton));

        #endregion

        #region Routed Event Handler Wrappers

        public event RoutedEventHandler SortModeChanged
        {
            add { AddHandler(SortModeChangedEvent, value); }
            remove { RemoveHandler(SortModeChangedEvent, value); }
        }

        #endregion
        
        #region Constructors

        static SortButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SortButton), new FrameworkPropertyMetadata(typeof(SortButton)));
        }

        #endregion

        #region Override Mehtods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            InitControls();
        }

        #endregion

        #region Dependency Property Changed Callback

        private static void OnCurrentSortModePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SortButton sortButton = d as SortButton;
            if (sortButton == null)
            {
                return;
            }

            SortMode sm = (SortMode)e.NewValue;
            if (sm == SortMode.None)
            {
                return;
            }

            if (sortButton._btnAsc != null)
            {
                sortButton._btnAsc.Visibility = (sm == SortMode.Asc ? Visibility.Visible : Visibility.Collapsed);
            }

            if (sortButton._btnDes != null)
            {
                sortButton._btnDes.Visibility = (sm == SortMode.Des ? Visibility.Visible : Visibility.Collapsed);
            }
        }

        #endregion

        #region Event Methods


        private void _btnAsc_Click(object sender, RoutedEventArgs e)
        {
            CurrentSortMode = SortMode.Des;
            RoutedEventArgs args = new RoutedEventArgs(SortModeChangedEvent, sender);
            RaiseEvent(args);
        }

        private void _btnDes_Click(object sender, RoutedEventArgs e)
        {
            CurrentSortMode = SortMode.Asc;
            RoutedEventArgs args = new RoutedEventArgs(SortModeChangedEvent, sender);
            RaiseEvent(args);
        }

        #endregion

        #region Private Methods

        private void InitControls()
        {
            _btnAsc = GetTemplateChild("PART_AscButton") as Button;
            if (_btnAsc != null)
            {
                _btnAsc.Click += _btnAsc_Click;
                if(CurrentSortMode == SortMode.Asc)
                {
                    _btnAsc.Visibility = Visibility.Visible;
                }
                else
                {
                    _btnAsc.Visibility = Visibility.Collapsed;
                }
            }

            _btnDes = GetTemplateChild("PART_DesButton") as Button;
            if (_btnDes != null)
            {
                _btnDes.Click += _btnDes_Click;
                if(CurrentSortMode == SortMode.Des)
                {
                    _btnDes.Visibility = Visibility.Visible;
                }
                else
                {
                    _btnDes.Visibility = Visibility.Collapsed;
                }
            }
        }

        #endregion

        #region Public Methdos
        #endregion
    }
}
